package Two.结构型模式.G3装饰器模式;

/**
 * @author 李某
 * * @date 2021/11/5
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */


    public class DecoratorPattern {
        public static void main(String[] args) {
            Component p = new ConcreteComponent();
            p.operation();
            System.out.println("---------------------------------");
            Component d = new ConcreteDecorator(p);
            d.operation();
        }
    }

    //抽象构件角色
    interface Component {
        public void operation();
    }

    //具体构件角色
    class ConcreteComponent implements Component {
        public ConcreteComponent() {
            System.out.println("创建具体构件角色");
        }

        public void operation() {
            System.out.println("调用具体构件角色的方法operation()");
        }
    }

    //抽象装饰角色
    class Decorator implements Component {
        private Component component;

        public Decorator(Component component) {
            this.component = component;
        }

        public void operation() {
            component.operation();
        }
    }

    //具体装饰角色
    class ConcreteDecorator extends Decorator {
        public ConcreteDecorator(Component component) {
            super(component);
        }

        public void operation() {
            super.operation();
            addedFunction();
        }

        public void addedFunction() {
            System.out.println("为具体构件角色增加额外的功能addedFunction()");
        }
    }

